package cn.hackcloud.mutithread;

/**
 * 线程交替打印
 * 
 * @author Created by laijun on 2018/12/07
 */
class PrintNum2 implements Runnable {
	int num = 1;

	@Override
	public void run() {
		while (true) {
			synchronized (this) {
				notifyAll();
				if (num <= 100) {
					System.out.println(Thread.currentThread().getName() + ":" + num);
					num++;
				} else {
					break;
				}
				try {
					wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

public class TestCondition {
	public static void main(String[] args) {
		PrintNum2 p = new PrintNum2();
		Thread t1 = new Thread(p);
		Thread t2 = new Thread(p);

		t1.setName("甲");
		t2.setName("乙");

		t1.start();
		t2.start();
	}
}
